package com.vg.view;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vg.com.chatonline.activity.R;
import vg.com.chatonline.adapter.adapter_friend;
import vg.com.classdata.yahoo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLJSONReponse;

public class FriendFragment extends Fragment {
	String url = "http://deduocyeu.com/json/getUserStatus.php?page=1&pagesize=14";
	ArrayList<yahoo> arr;
	adapter_friend adapter;
	private Handler mhHandler, mtime;
	private ListView lv;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.listview_ya, null);
		lv = (ListView) v.findViewById(R.id.listview);
		arr = new ArrayList<yahoo>();
		mhHandler = new Handler();

		URLRequest request = new URLRequest(url, new RequestDelegate());
		request.setReponse(new URLJSONReponse());
		request.setCachePolicy(URLCachePolicy.DISK);
		request.send();
		return v;

	}

	public static Fragment newinstance() {
		return new FriendFragment();

	}

	private class RequestDelegate extends URLRequestDelegate {
		@Override
		public void onRequestReponse(final URLRequest httpRequest) {
			mhHandler.post(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					// URLBitmapResponse
					// reponse=(URLBitmapResponse)httpRequest.getReponse();
					// img.setImageBitmap(reponse.bitmap);

					URLJSONReponse response = (URLJSONReponse) httpRequest
							.getReponse();

					JSONArray jarr;
					try {
						jarr = response.jsonObj.getJSONArray("data");
						for (int i = 0; i < 20; i++) {
							JSONObject obj = jarr.getJSONObject(i);
							String name_test = obj.getString("use_fullname");
							String Status_test = obj.getString("use_status");
							String img = obj.getString("use_avatar");
							String status = obj.getString("use_online");

							int img_status;
							if (status.equals("0")) {
								img_status = R.drawable.off;
							} else {
								img_status = R.drawable.on;
							}

							Log.i("tuan", "" + img_status);
							yahoo yahoo = new yahoo("", name_test, img,
									img_status);
							arr.add(yahoo);
							adapter = new adapter_friend(getActivity(), arr);

						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					lv.setAdapter(adapter);
					lv.setFastScrollEnabled(true);
					adapter.notifyDataSetChanged();
					lv.setOnItemSelectedListener(new OnItemSelectedListener() {

						public void onItemSelected(AdapterView<?> arg0,
								View arg1, int arg2, long arg3) {
							// TODO Auto-generated method stub
							Log.i("click", "true");
						}

						public void onNothingSelected(AdapterView<?> arg0) {
							// TODO Auto-generated method stub

						}
					});
				}
			});

			mtime = new Handler();
			mtime.postDelayed((Runnable) mhHandler, 2000);
			adapter.notifyDataSetChanged();

		}

		// anh hien thi khi timeout
		@Override
		public void onRequestError(URLRequest httpRequest, Throwable error) {
			// TODO Auto-generated method stub

		}

		// chay tu dau la anh gi khi load xong se thay doi
		@Override
		public void onRequestStart(URLRequest httpRequest) {
			// TODO Auto-generated method stub
			super.onRequestStart(httpRequest);
		}
	}

}
